package com.shaoshaohuo.app.ui.ec;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.QuoteDetail;
import com.shaoshaohuo.app.entity.QuoteDetailEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.view.TopbarView;

public class QuoteDetailActivity extends BaseActivity implements OnClickListener {

	private TopbarView mTopbarView;

	private TextView mPriceText;
	private TextView mSupplyNameText;
	private TextView mContactNameText;
	private TextView mContactPhoneText;

	private Button mNextButton;

	private String id;
	private String orderid;

	private QuoteDetail data;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(new TopbarView(this));
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				setContentView(R.layout.activity_quote_detail);
				initData();
				initView();
				setUpView();
				reqeustData();
			}
		}, 200);
	}

	protected void initData() {
		Intent intent = getIntent();
		id = intent.getStringExtra(ExtraName.id);
		orderid = intent.getStringExtra(ExtraName.orderid);
	}

	protected void reqeustData() {
		startLoadingDialog();
		RequestService.getInstance().getQuoDetail(this, id, orderid, QuoteDetailEntity.class, new RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if (resultData.isOk()) {
					QuoteDetailEntity entity = (QuoteDetailEntity) resultData;
					updateUi(entity);
				} else {
					showToast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {

			}
		});
	}

	protected void updateUi(QuoteDetailEntity entity) {
		data = entity.getData();
		mPriceText.setText("￥"+data.getPrice()+"元");
		mSupplyNameText.setText("供应商名称："+data.getSuppliers());
		mContactNameText.setText("联系人："+data.getRealname());
		mContactPhoneText.setText("联系电话："+data.getPhone());
		
		if("0".equals(data.getStatus())){
			mNextButton.setVisibility(View.VISIBLE);
		}else{
			mNextButton.setVisibility(View.GONE);
		}
	}

	
	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		mNextButton = (Button) this.findViewById(R.id.button_next);
		
		mPriceText = (TextView) this.findViewById(R.id.textview_price);
		mSupplyNameText = (TextView) this.findViewById(R.id.textview_supply_name);
		mContactNameText = (TextView) this.findViewById(R.id.textview_contact_name);
		mContactPhoneText = (TextView) this.findViewById(R.id.textview_contact_phone);
	}

	private void setUpView() {
		mTopbarView.setCenterText("报价详情");
		mTopbarView.setLeftView(true, true);
		mTopbarView.setRightText("取消订单");
		mTopbarView.setRightClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                showCancelDialog();
            }
        });

		mNextButton.setOnClickListener(this);
		mNextButton.setVisibility(View.GONE);
	}

	private void showCancelDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确认取消订单?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                cancelQuote();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.create().show();
    }
	
	protected void cancelQuote() {
	    startLoadingDialog();
	    RequestService.getInstance().cancelPurchaseOrder(this, orderid, BaseEntity.class, new RequestListener() {
            
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if(resultData.isOk()){
                    showToast("取消订单成功");
//                    mTopbarView.setRightText("");
//                    mNextButton.setVisibility(View.GONE);
                    finish();
                }else{
                    showToast(resultData.getMsg());
                }
            }
            
            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }

    @Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_next:
			showConfimDialog();
			break;
		}
	}

	private void showConfimDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("确认选择此供货商?");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				selectSupplier();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		builder.create().show();
	}

	protected void selectSupplier() {
		startLoadingDialog();
		RequestService.getInstance().selectQuote(this, id, orderid, BaseEntity.class, new RequestListener() {
			
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if(resultData.isOk()){
					showToast("选择供应商成功");
//					reqeustData();
					toOrderDetail();
				}else{
					showToast(resultData.getMsg());
				}
			}
			
			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(R.string.please_check_network);
			}
		});
		
//		Intent intent = new Intent(this, PurchaseOrderDetailActivity.class);
//		startActivity(intent);
	}

    protected void toOrderDetail() {
//        Intent intent = new Intent(this, PurchaseOrderDetailActivity.class);
//        intent.putExtra(ExtraName.orderid, orderid);
//        startActivity(intent);
        
        Intent intent = new Intent(this, PayEcActivity.class);
		intent.putExtra(ExtraName.orderid, orderid);
		intent.putExtra(ExtraName.totalPrice, Double.parseDouble(data.getTotal()));
		intent.putExtra(ExtraName.action, PayEcActivity.PAY_ACTION_SUPPLY);
		startActivity(intent);
		finish();
    }

}
